﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Data;
using System.IO;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using System.Xml;
using System.Linq;
using System.Windows.Input;
using System.Windows.Controls.Primitives;

namespace UniversalEditor.Csv
{
    public partial class TableContainer
    {
        ObservableCollection<DataRowView> _Fields = new ObservableCollection<DataRowView>();

        public ObservableCollection<DataRowView> FieldsProperty
        {
            get { return _Fields; }
            set { _Fields = value; }
        }

        public TableContainer()
        {
            InitializeComponent();
        }

        private void OnLoadingRow(object sender, DataGridRowEventArgs e)
        {
            if (!e.Row.IsNewItem) e.Row.Header = (e.Row.GetIndex() + 1).ToString();
            else e.Row.Header = string.Empty;
        }

        private void DataGrid_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
        {
            if (!IsLoaded) e.Row.Header = "1";
            else e.Row.Header = (e.Row.GetIndex() + 1).ToString();
        }

        private void dtGrid_ScrollChanged(object sender, ScrollChangedEventArgs e)
        {
            if (e.HorizontalChange != 0 & e.HorizontalChange > 0) { HorizontalChanged(); }
            else if (e.VerticalChange != 0 & e.VerticalChange > 0) { VerticalChanged(); }
        }

        private void VerticalChanged()
        {
            DataRowView firstRow = FieldsProperty.First() as DataRowView;
            string[] currentRowArray = new string[FieldsProperty.First().Row.ItemArray.Length];
            for (int j = 0; j < firstRow.Row.ItemArray.Length; j++) { currentRowArray[j] = string.Empty; }
            DataTable dt = FieldsProperty.Last().Row.Table;
            dt.Rows.Add(currentRowArray);
            FieldsProperty.Add(dt.AsDataView()[dt.AsDataView().Count - 1]);
        }

        private void HorizontalChanged()
        {
            int ColCount = FieldsProperty[0].Row.ItemArray.Length;
            int x = ColCount % 26; int y = (ColCount - 26) / 26;
            char FLiteral = Convert.ToChar(y + Convert.ToInt32('A'));
            char SLiteral = Convert.ToChar(x + Convert.ToInt32('A'));
            string Header = (y > 25 ? string.Empty : FLiteral.ToString() + SLiteral);
            i_CsvDG.Columns.Add(new DataGridTextColumn() { Header = Header, Binding = new Binding(Header) });
            System.Data.DataColumn newColumn = new System.Data.DataColumn(Header);
            newColumn.DefaultValue = string.Empty;
            FieldsProperty.First().Row.Table.Columns.Add(newColumn);
        }

        private void i_CsvDG_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
        {
            if (e.Column.Header.ToString() == "DataView") e.Cancel = true;
            else if (e.Column.Header.ToString() == "RowVersion") e.Cancel = true;
            else if (e.Column.Header.ToString() == "Row") e.Cancel = true;
            else if (e.Column.Header.ToString() == "IsNew") e.Cancel = true;
            else if (e.Column.Header.ToString() == "IsEdit") e.Cancel = true;
        }
    }
}
